<script>
  import config from '@/app.config.js'
  import { version } from './package.json'
  import silenceUpdate from '@/uni_modules/rt-uni-update/js_sdk/silence-update.js'
  export default {
    methods: {},
    // 监听 - 页面404
    // 监听 - 页面404
    onPageNotFound: function (e) {
      uni.redirectTo({
        url: config.error.url
      })
    },
    onLaunch: function (options) {
      console.log('onLaunch==> ', onLaunch)
      //app 安全验证
      vk.myfn.secure()
      //刷新token
      vk.myfn.refreshToken()
      // 一键登录初始化
      // #ifdef APP-PLUS

      if (!vk.getVuex('$user.userInfo.id')) {
        uni.preLogin({
          provider: 'univerify',
          success(res) {
            console.log('预登录成功==> ', res)
            vk.setVuex('$user.preLoginInfo', true)
          },
          fail(err) {
            console.log('预登录失败==> ', err)
            vk.setVuex('$user.preLoginInfo', false)
          }
        })
      }
      // 网络初始化
      uni.getNetworkType({
        success: res => {
          let networkType = res.networkType !== 'none'
          console.log('networkType==> ', networkType)
          vk.setVuex('$app.networkType', networkType)
          if (networkType) vk.myfn.getLocation()
        }
      })
      // #endif
      uni.getSystemInfo({
        success: function (e) {
          // mark StatusBar 手机状态栏
          // mark CustomBar 微信浏览器状态栏
          vk.setVuex('$app.windowHeight', e.windowHeight)

          // #ifndef MP
          vk.setVuex('$app.StatusBar', e.statusBarHeight)

          if (e.platform == 'android') {
            vk.setVuex('$app.CustomBar', e.statusBarHeight + 50)
          } else {
            vk.setVuex('$app.CustomBar', e.statusBarHeight + 45)
          }
          // #endif

          // #ifdef MP-WEIXIN

          vk.setVuex('$app.StatusBar', e.statusBarHeight)

          let custom = wx.getMenuButtonBoundingClientRect()
          vk.setVuex('$app.Custom', custom)

          vk.setVuex('$app.CustomBar', custom.bottom + custom.top - e.statusBarHeight)

          // #endif

          // #ifdef MP-ALIPAY
          vk.setVuex('$app.StatusBar', e.statusBarHeight)

          vk.setVuex('$app.CustomBar', e.statusBarHeight + e.titleBarHeight)

          // #endif
        }
      })
      // error ：config.debug 在正式环境时，值为false，故此{}内的代码只有开发环境才会执行
      if (config.debug) {
        // #ifndef APP-PLUS
        console.log(
          `%c vk-client %c v${version} `,
          'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
          'background:#007aff ;padding: 1px; border-radius: 0 3px 3px 0;  color: #fff; font-weight: bold;'
        )
        // #endif
        console.log('App Launch')
      }
      // error：以下代码正式和开发环境都会执行
      // #ifdef MP
      uni.vk.updateManager.updateReady() // 此代码可以让小程序自动检测最新版本
      // #endif
    },

    onShow: function () {
      // #ifdef APP-PLUS
      console.log('onShow==> ', 'onShow')

      const { locationAuthorized } = uni.getAppAuthorizeSetting()
      console.log('locationAuthorized==> ', locationAuthorized)

      let type = vk.getVuex('$app.checkLocationPermission.type')

      // 存储定位定位状态
      vk.setVuex('$app.checkLocationPermission', {
        isOpen: locationAuthorized === 'authorized',
        type: locationAuthorized
      })
      // 下次询问主动拉起定位授权
      if (locationAuthorized === 'not determined') {
        vk.myfn.getLocation()
      }
      // 上次关闭定位 切换之后 定位开启了   主动拉起定位授权
      if (type == 'denied' && locationAuthorized == 'authorized') {
        vk.myfn.getLocation()
      }

      uni.vk.vuex.dispatch('$app/getInitData')
      //

      // #endif
    },
    onHide: function () {
      if (config.debug) console.log('App Hide')
    }
  }
</script>

<style lang="scss">
  @import './uni_modules/vk-uview-ui/index.scss';
  // @import './common/css/main.css';
  @import './common/css/icon.css';
  @import './common/css/weather.css';

  @import 'tailwindcss/base';
  @import 'tailwindcss/utilities';
  @import 'tailwindcss/components';

  .page {
    background-color: #f4f6f8 !important;
    font-family: sans-serif !important;
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
  }

  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }

  .fade-enter-from,
  .fade-leave-to {
    opacity: 0;
  }

  .pb-env {
    padding-bottom: calc(env(safe-area-inset-bottom) / 2);
  }

  @font-face {
    font-family: 'oxanium';
    src: url('font/Oxanium-Bold.ttf');
  }

  // .zp-page-bottom-container {
  //   box-shadow: 0 -0.375rem 0.75rem -0.125rem rgba(0, 0, 0, 0.1);
  //   // z-index: 9999999;
  //   border-radius: 1rem 1rem 0 0;
  //   background-color: transparent;
  // }

  // .btn {
  //   @apply active:scale-95 duration-200 transition-all;
  //  }
  // <view class="pb-env"></view>
</style>
